package repository

import (
	"github.com/gccai/write_helper/model"
	"github.com/jinzhu/gorm"
	"time"
)

type ChapterRepository struct {
	DB *gorm.DB
}

type ChapterRepositoryInterface interface {
	Add(chapter *model.Chapter) (*model.Chapter, error)
	Delete(chapter *model.Chapter) (bool, error)
	Edit(chapter *model.Chapter) *model.Chapter
	GetChapterCount(chapter *model.Chapter) (int64, error)
	GetChapterContent(chapter *model.Chapter) (*model.Chapter, error)
	GetChapters(chapter *model.Chapter) ([]*model.Chapter, error)
	Exist(num string) (bool, error)
}

func (c ChapterRepository) Add(chapter *model.Chapter) (*model.Chapter, error) {
	//if chapter.ChapterContent == "" || chapter.ChapterName == "" {
	//	return nil, errors.New("章节名或者章节内容不能为空。")
	//}
	err := c.DB.Create(&chapter).Error
	if err != nil {
		return nil, err
	}
	return chapter, nil
}

func (c ChapterRepository) Delete(chapter *model.Chapter) (bool, error) {
	err := c.DB.Model(chapter).Where("user_id = ? and book_name = ? and chapter_id = ? and is_deleted = ?",
		chapter.UserId, chapter.BookName, chapter.ChapterId, false).Updates(map[string]interface{}{
		"is_deleted": true,
		"delete_at":  time.Now(),
	}).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

func (c ChapterRepository) Edit(chapter *model.Chapter) *model.Chapter {
	err := c.DB.Model(chapter).Where("user_id = ? and book_name = ? and chapter_id = ? and is_deleted = ?",
		chapter.UserId, chapter.BookName, chapter.ChapterId, false).Updates(map[string]interface{}{
		"chapter_name":       chapter.ChapterName,
		"chapter_story_core": chapter.ChapterStoryCore,
		"chapter_content":    chapter.ChapterContent,
	}).Error
	if err != nil {
		return nil
	}
	return chapter
}

func (c ChapterRepository) GetChapterCount(chapter *model.Chapter) (int64, error) {
	var count int64
	err := c.DB.Where("user_id = ? and book_name = ? and chapter_id = ? and is_deleted = ?", chapter.UserId, chapter.BookName, chapter.ChapterId, false).Find(chapter).Count(&count).Error
	if err != nil {
		return 0, err
	}
	return count, nil
}

func (c ChapterRepository) GetChapterContent(chapter *model.Chapter) (*model.Chapter, error) {
	var chapterContent model.Chapter
	err := c.DB.Where("user_id = ? and book_name = ? and chapter_id = ? and is_deleted = ?",
		chapter.UserId, chapter.BookName, chapter.ChapterId, false).Find(&chapterContent).Error
	if err != nil {
		return nil, err
	}
	return &chapterContent, nil
}

func (c ChapterRepository) GetChapters(chapter *model.Chapter) ([]*model.Chapter, error) {
	var chapters []*model.Chapter
	err := c.DB.Where("chapter_id = ? and book_name = ? and is_deleted = ?",
		chapter.UserId, chapter.BookName, chapter.IsDeleted).Find(&chapters).Error
	if err != nil {
		return nil, err
	}
	return chapters, nil
}

func (c ChapterRepository) Exist(num string) (bool, error) {
	err := c.DB.Where("chapter_id = ? and is_deleted = ?", num, false).Find(&model.Chapter{}).Error
	if err != nil {
		return false, err
	}
	return true, nil
}
